﻿using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Entities;

namespace ServicesDirectory.UserControl
{
    public partial class ListDepartment : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            All.Click += All_Click;
            lbt09.Click += lbt09_Click;
            lbtA.Click += lbtA_Click;
            lbtF.Click += lbtF_Click;
            lbtK.Click += lbtK_Click;
            lbtO.Click += lbtO_Click;
            lbtS.Click += lbtS_Click;
            lbtW.Click += lbtW_Click;
            LbtName.Click += LbtName_Click;
            chkInactive.CheckedChanged += ChkInactive_CheckedChanged;
            if (!IsPostBack)
            {
                Display();
            }
        }

        private void ChkInactive_CheckedChanged(object sender, EventArgs e)
        {
            Display();
        }

        #region filter funtion - do filfer base on keyword

        /// <summary>
        /// Handles the Click event of the lbtW control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtW_Click(object sender, EventArgs e)
        {
            ViewState["filter"] = "wz";
            ViewState["btnFilter"] = "lbtW";
            Display();
        }

        /// <summary>
        /// Handles the Click event of the lbtS control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtS_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "lbtS";
            ViewState["filter"] = "sv";
            Display();
        }

        /// <summary>
        /// Handles the Click event of the lbtO control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtO_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "lbtO";
            ViewState["filter"] = "or";
            Display();
        }

        /// <summary>
        /// Handles the Click event of the lbtK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtK_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "lbtK";
            ViewState["filter"] = "kn";
            Display();
        }


        /// <summary>
        /// Handles the Click event of the lbtF control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtF_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "lbtF";
            ViewState["filter"] = "fj";
            Display();
        }


        /// <summary>
        /// Handles the Click event of the lbtA control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtA_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "lbtA";
            ViewState["filter"] = "ae";
            Display();
        }


        /// <summary>
        /// Handles the Click event of the lbt09 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbt09_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "lbt09";
            ViewState["filter"] = "09";
            Display();
        }



        /// <summary>
        /// Handles the Click event of the All control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void All_Click(object sender, EventArgs e)
        {
            ViewState["btnFilter"] = "All";
            ViewState["filter"] = "all";
            Display();
        }

        #endregion

        #region paging button
        /// <summary>
        /// Handles the Click1 event of the CmdFisrt control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdFisrt_Click1(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            Display();
        }

        /// <summary>
        /// Handles the Click1 event of the CmdBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdBack_Click1(object sender, EventArgs e)
        {
            if(Int32.Parse(txtPageNumber.Text,CultureInfo.CurrentCulture)>1)
            {
                txtPageNumber.Text = (Int32.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) - 1).ToString(CultureInfo.CurrentCulture);
            }
            Display();
        }

        /// <summary>
        /// Handles the Click1 event of the CmdNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdNext_Click1(object sender, EventArgs e)
        {
            if(Int32.Parse(txtPageNumber.Text,CultureInfo.CurrentCulture)<Int32.Parse(lblTotalPage.Text,CultureInfo.CurrentCulture))
            {
                txtPageNumber.Text =
                    (Int32.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) + 1).ToString(
                        CultureInfo.CurrentCulture);
            }
            Display();
        }

        /// <summary>
        /// Handles the Click1 event of the CmdLast control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdLast_Click1(object sender, EventArgs e)
        {
            txtPageNumber.Text = lblTotalPage.Text;
            Display();
        }
        #endregion

        #region function
        protected void BtnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddDepartment.aspx");
        }
        
        private void Display()
        {
            if (ViewState["btnFilter"] != null)
            {
                ((LinkButton)FindControl(ViewState["btnFilter"].ToString())).ForeColor = Color.FromName("#99ff00");
            }
            if (Session["directorateid"] != null)
            {
                var directorateid = new Guid(Session["directorateid"].ToString());
                Collection<Department> colDepartments;

                if (ViewState["filter"] == null)
                {
                    colDepartments = DepartmentBusiness.Instance.Filter("all", chkInactive.Checked, directorateid);
                }
                else
                {
                    colDepartments = DepartmentBusiness.Instance.Filter(ViewState["filter"].ToString(),
                                                                        chkInactive.Checked, directorateid);
                }

                if (ViewState["sort"] != null)
                {
                    if (ViewState["sort"].ToString().Equals("asc"))
                    {
                        colDepartments = new Collection<Department>(colDepartments.OrderBy(item => item.Name).ToList());
                    }
                    else
                    {
                        colDepartments =
                            new Collection<Department>(colDepartments.OrderByDescending(item => item.Name).ToList());
                    }
                }
                Paging(colDepartments);
            }
        }

        /// <summary>
        /// Handles the ItemCommand event of the Repeater1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterCommandEventArgs" /> instance containing the event data.</param>
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                var lblIsactive = e.Item.FindControl("lblIsactive") as Label;
                if (lblIsactive != null)
                {
                    if (lblIsactive.Text == "Yes")
                    {
                        Session["departmentid"] = e.CommandArgument.ToString();
                        Response.Redirect("DetailDepartment.aspx");
                    }
                    else
                    {
                        var script = "var result= window.confirm('Do you want to make this Department active?'); " +
                                     "if(result)" +
                                     " location.href=('DetailDepartment.aspx?active=" + e.CommandArgument + "');";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1,typeof(UpdatePanel),UpdatePanel1.ClientID,script,true);
                    }
                }
                
            }
        }

        /// <summary>
        /// Handles the ItemDataBound event of the Repeater1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var lblIsactive = e.Item.FindControl("lblIsactive") as Label;
            if (lblIsactive != null)
            {
                if (lblIsactive.Text == "True")
                {
                    lblIsactive.Text = "Yes";
                }
                else
                {
                    lblIsactive.Text = "No";
                }
            }
        }

        /// <summary>
        /// Pagings the specified list departments.
        /// </summary>
        /// <param name="listDepartments">The list departments.</param>
        private void Paging(Collection<Department> listDepartments)
        {
            lblTotalPage.Text = (listDepartments.Count % 15 == 0 ? listDepartments.Count / 15 : listDepartments.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
            //paging on list result
            var pagedList = DepartmentBusiness.Instance.Paging(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, listDepartments);
            //bind paged data
            Repeater1.DataSource = pagedList;
            Repeater1.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the LbtName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtName_Click(object sender, EventArgs e)
        {
            if(ViewState["sort"] == null || ViewState["sort"].Equals("desc"))
            {
                ViewState["sort"] = "asc";
            }
            else
            {
                ViewState["sort"] = "desc";
            }
            Display();
        }
        #endregion
    }
}